package com.tianyang.yukuaifu.common.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ScrollView;

/**
 * Description：重写ScrollView,以解决ScrollView与水平listView滑动时冲突
 * Created by 薛勇军 on 2018/8/3.
 */

public class CustomScrollView extends ScrollView {

    private GestureDetector mGestureDetector;

    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGestureDetector = new GestureDetector(new YScrollDetector());
        setFadingEdgeLength(0);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return super.onInterceptTouchEvent(ev)
                && mGestureDetector.onTouchEvent(ev);
    }

    class YScrollDetector extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                                float distanceX, float distanceY) {
            if (Math.abs(distanceY) > Math.abs(distanceX)) {
                return true;
            }
            return false;
        }
    }

    public static interface ScrollChangedCallBack{
        void onChange(int x, int y, int oldX, int oldY);
    }

    private ScrollChangedCallBack onScrollChangedListener;
    public void setOnScrollChangedListener(ScrollChangedCallBack onScrollChangedListener){
        this.onScrollChangedListener = onScrollChangedListener;
    }
    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        // l oldl 分别代表水平位移
        // t oldt 代表当前左上角距离Scrollview顶点的距离
        super.onScrollChanged(l, t, oldl, oldt);
        if(null != onScrollChangedListener){
            onScrollChangedListener.onChange(l,t,oldl,oldt);
        }
    }
}
